{% extends 'layout/base.html' %}

{% block content %}
<div class="row mt-5">
    <div class="col-lg">
        <h2>Review CSV Import</h2>

        {% if errors|length > 0 %}
        <h4 class="text-danger">{{ errors|length }} Error(s)</h4>
        <div class="pb-3">Please fix the below errors, and re-upload the updated file.</div>
        <table class="table table-sm table-striped table-responsive-lg">
            <thead>
            <tr>
                <th>Row</th>
                <th>Error</th>
            </tr>
            </thead>
            <tbody>
            {% for error in errors %}
            <tr>
                <td class="text-center">{{ error.row }}</td>
                <td style="width: 100%;">{{ error.error }}</td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
        {% else %}
            <div class="clearfix">
                <div class="float-right">
                    <form action="{{ url_for('dns.zones_import_run') }}" method="post">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        <button type="submit" class="btn btn-sm btn-warning">import data</button>
                    </form>
                </div>
            </div>

            {% if data|length > 0 %}

                {% if type == 1 %}
                    <h4 class="text-info">{{ data|length }} Zone(s) to Import</h4>
                    <table class="table table-sm table-striped table-responsive-lg">
                        <thead>
                        <tr>
                            <th class="text-center">#</th>
                            <th class="text-center">Action</th>
                            <th>Domain</th>
                            <th class="text-center">Active</th>
                            <th class="text-center" style="white-space: nowrap;">Catch-All</th>
                            <th class="text-center">Forwarding</th>
                            <th class="text-center">Regex</th>
                            <th class="text-center">Master</th>
                            <th>Tags</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for zone in data %}
                        <tr>
                            <td class="text-center">{{ loop.index }}</td>
                            <td class="text-center">{{ 'Create' if zone.id == 0 else 'Update' }}</td>
                            <td style="width: 100%;">{{ zone.domain }}</td>
                            <td class="text-center">
                                {% set class = 'success' if zone.active else 'danger' %}
                                {% set icon = 'check' if zone.active else 'times' %}
                                <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                            </td>
                            <td class="text-center">
                                {% set class = 'success' if zone.catch_all else 'danger' %}
                                {% set icon = 'check' if zone.catch_all else 'times' %}
                                <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                            </td>
                            <td class="text-center">
                                {% set class = 'success' if zone.forwarding else 'danger' %}
                                {% set icon = 'check' if zone.forwarding else 'times' %}
                                <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                            </td>
                            <td class="text-center">
                                {% set class = 'success' if zone.regex else 'danger' %}
                                {% set icon = 'check' if zone.regex else 'times' %}
                                <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                            </td>
                            <td class="text-center">
                                {% set class = 'success' if zone.master else 'danger' %}
                                {% set icon = 'check' if zone.master else 'times' %}
                                <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                            </td>
                            <td style="white-space: nowrap">
                                {{ ', '.join(zone.tags) }}
                            </td>
                        </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                {% elif type == 2 %}
                    <h4 class="text-info">{{ data|length }} Record(s) to Import</h4>
                    <table class="table table-sm table-striped table-responsive-lg">
                        <thead>
                        <tr>
                            <th class="text-center">#</th>
                            <th class="text-center">Action</th>
                            <th>Domain</th>
                            <th class="text-center">Active</th>
                            <th>Type</th>
                            <th>Class</th>
                            <th class="text-right">TTL</th>
                            <th>Properties</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for record in data %}
                        <tr>
                            <td class="text-center">{{ loop.index }}</td>
                            <td class="text-center">{{ 'Create' if record.record_id == 0 else 'Update' }}</td>
                            <td style="width: 100%;">{{ record.domain }}</td>
                            <td class="text-center">
                                {% set class = 'success' if record.active else 'danger' %}
                                {% set icon = 'check' if record.active else 'times' %}
                                <span class="text-{{ class }}"><i class="fas fa-{{ icon }} fa-fw"></i></span>
                            </td>
                            <td>{{ record.type }}</td>
                            <td>{{ record.cls }}</td>
                            <td class="text-right">{{ record.ttl }}</td>
                            <td style="white-space: nowrap;">
                                {% for name, value in record.data.items() %}
                                <div>{{ name }}: {{value}}</div>
                                {% endfor %}
                            </td>
                        </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                {% endif %}
            {% endif %}
        {% endif %}
    </div>
</div>
{% endblock %}